<template>
  <el-form :model="timeForm" :inline="true" size="small" class="custom-scrollbar">
    <el-form-item label="">
      <el-input-number v-model="timeForm.time" :min="1000" :step="1000" size="small" controls-position="right"
        @change="handleChange" class="!w-150px" />
      <span v-if="!isEdit" class="name">{{ timeForm.name }} <el-icon @click="isEdit = !isEdit">
          <EditPen />
        </el-icon></span>
      <el-input v-else v-model="timeForm.name" class="!w-150px">
        <template #append>
          <el-button :icon="Check" type="primary" @click="changeName" />
        </template>
      </el-input>
    </el-form-item>
  </el-form>
</template>

<script setup lang="ts">
import { EditPen, Check } from '@element-plus/icons-vue'
import { propTypes } from '@/utils/propTypes'
const props = defineProps({
  data: propTypes.object.def({})
})
const emit = defineEmits(['time-form'])
const isEdit = ref(false)
const timeForm = reactive({
  time: 1,
  name: '等待时间'
})
/** 更新等待时间后，向上传递数据 */
const handleChange = () => {
  emit('time-form', timeForm, props.data)
}
/** 更新名称后，重置编辑状态，向上传递数据 */
const changeName = () => {
  isEdit.value = false
  emit('time-form', timeForm, props.data)
}

onMounted(() => {
  //数据回显 
  if (props.data.name) {
    timeForm.name = props.data.name
    timeForm.time = JSON.parse(props.data.config).delay
  }
})
</script>

<style scoped lang="scss">
.name {
  margin-left: 10px;

  .el-icon {
    display: none;
  }

  &:hover {
    .el-icon {
      display: inline-block
    }
  }
}

.el-input {
  margin-left: 10px;
}
</style>
